/*
 * spa.components.js
 * auther
 * root namespace module
 */

/*jslint           browser : true,     continue : true,
   devel : true,    indent : 2,          maxerr : 10,
  newcpa : true,     nomen : true,      plugsplus : true,
  regexp : true,    sloppy : true,
 */

/* global : $,spa */
spa.components = (function($){
    var
        Navigater
    // ------------------------------ BEGIN MODULE SCOPE VARIABLES -----------------------------
    // ------------------------------ END MODULE SCOPE VARIABLES -----------------------------

    // ------------------------------ BEGIN UTILITY METHODS ----------------------------------
    // ------------------------------ BEGIN UTILITY METHODS ----------------------------------

    // ------------------------------ BEGIN DOM METHODS ----------------------------------
    // ------------------------------ END DOM METHODS ----------------------------------

    // ------------------------------ BEGIN EVENT METHODS ----------------------------------
    // ------------------------------ END EVENT METHODS ----------------------------------

    // ------------------------------ BEGIN PUBLIC METHODS ----------------------------------
    Navigater = (function(options){
        var
            configMap = {
                main_html : String()
                    +'<nav class="navbar navbar-fixed-top navbar-inverse navbar-embossed" role="navigation">'
                        +'<div class="navbar-header">'
                            +'<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">'
                                +'<span class="sr-only">导航切换</span>'
                            +'</button>'
                            +'<a class="navbar-brand" href="#"></a>'
                        +'</div>' +
                        +'<div class="collapse navbar-collapse">'
                        +'</div>'
                    +'</nav>',
                settable_map : {
                    title : true
                }
            },
            stateMap = {
                $container : null
            },
            jqueryMap = {
                $container : null
            },
            configModule,initModule,setJqueryMap

        // ------------------------------ BEGIN DOM METHODS ----------------------------------
        // Begin DOM method /setJqueryMap/
        //

        setJqueryMap = function(){
            var
                $container = stateMap.$container
            jqueryMap.$container = $container
        }

        // End DOM method /setJqueryMap/
        // ------------------------------ END DOM METHODS ----------------------------------

        // ------------------------------ BEGIN PUBLIC METHODS ----------------------------------
        // Begin public method /initModule/
        //

        initModule = function($contianer){
            stateMap.$container = $container
            setJqueryMap()
        }
        // End public method /initModule/

        // Begin public method /configModule/
        //

        configModule = function(options){
            spa.util.setConfigModule({
                input_map       : options,
                settable_map    : configMap.settable_map,
                config_map      : configMap
            })
        }
        // End public method /configModule/
        // ------------------------------ END PUBLIC METHODS ----------------------------------

        return {
              initModule    : initModule,
            configModule    : configModule
        }
    }())
    // ------------------------------ END PUBLIC METHODS ----------------------------------

    return {
        Navigater : Navigater
    }
}(jQuery))

spa.components.Menu = function(options){
    var make_sub_menu,make_sub_menu_item,
        $menu = $("<ul/>"),
        menu_class = options.is_right ? 'nav navbar-nav navbar-right' : 'nav navbar-nav navbar-left'

    $menu.addClass(menu_class)

    make_sub_menu_item = function(arg_amp){
        var
            $sub_menu_item = $('<li><a><span class="title"></span><span class="caret"></span></a><ul/></li>'),
            $a = $sub_menu_item.find('a'),
            attrs = arg_map.attrs ? arg_map.attrs : {},
            children = arg_map.children ? arg_map.children : [],
            child,index,result,
            $sub_menu = $sub_menu_item.find('ul')

        $sub_menu_item.addClass('dropdown')
        attrs['data-toggle'] = 'dropdown'
        $a.text(arg_map.title)
            .attr(attrs)
            .addClass('dropdown-toggle')

        for(index in children){
            child = children[index]
            if(child.hasOwnProperty('children')){
                result = make_sub_menu_item($sub_menu,child)
            } else {
                result = make_menu_item($sub_menu,arg_map)
            }
            $sub_menu.append(result)
        }
        return $sub_menu_item
    }

    make_menu_item = function(arg_map){
        var
            $menu_item = $('<li><a></a></li>'),
            $a = $menu_item.find('a'),
            title = arg_map.title,
            attrs = arg_map.attrs ? arg_map.attrs : {}
            attrs['menu-item-name'] = arg_map.name
        $a.text(title)
            .attr(attrs)

        return $menu_item
    }
}

